/**
 * Copyright 2011 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.appengine.codelab;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PropertyProjection;
import com.google.appengine.api.datastore.Query;

/**
 * This class defines the methods for basic operations of create, update & retrieve
 * for the midia entity
 * 
 * @author
 *
 */
public class Midia {

	/**
	 * Update or create the midia
	 * 
	 * @param midia_name
	 *          : name of the midia
	 * @param midia_description
	 *          : description of the Midia
	 * @param midia_number
	 *          : number card of the Midia
	 * @param midia_bandeira
	 *          : bandeira of the Midia
	 * @return updated midia
	 */
  public static void createOrUpdateMidia(String midia_id, String midia_name, String midia_description,String midia_number, String midia_bandeira, String cliente_id) {
    Entity midia = getSingleMidia(midia_name);
    Entity cliente = Cliente.getSingleCliente(cliente_id);
    if (midia == null) {
      midia = new Entity("Midia", midia_id);
      midia.setProperty("midia_id", midia_id);
      midia.setProperty("midia_name", midia_name);
      midia.setProperty("midia_description", midia_description);
      midia.setProperty("midia_number", midia_number);
      midia.setProperty("midia_bandeira", midia_bandeira);
      midia.setProperty("cliente_id", cliente.getProperty("cliente_id").toString());
    } else {
    	if (midia_name != null && !"".equals(midia_name)) {
    		  midia.setProperty("midia_name", midia_name);
          }
          if (midia_description != null && !"".equals(midia_description)) {
        	  midia.setProperty("midia_description", midia_description);
          }
          if (midia_number != null && !"".equals(midia_number)) {
        	  midia.setProperty("midia_number", midia_number);
          }
          if (midia_bandeira != null && !"".equals(midia_bandeira)) {
        	  midia.setProperty("midia_bandeira", midia_bandeira);
          }
    }
    Util.persistEntity(midia);
  }

  public static List<String> listarBandeiras()
  {
	    Query query = new Query("Midia");
	    query.addProjection(new PropertyProjection("midia_bandeira", String.class));
	    query.setDistinct(true);

	    Iterable<Entity> iterable = DatastoreServiceFactory.getDatastoreService().prepare(query).asIterable();
	    List<String> bandeiras = new ArrayList<String>();
	    
	    for( Entity entity : iterable ){
	    	bandeiras.add( (String) entity.getProperty("midia_bandeira"));
	    }
	    
	    return bandeiras;
  } 
  
	/**
	 * Return all the midias
	 * 
	 * @param kind
	 *          : of kind midia
	 * @return midias
	 */
  public static Iterable<Entity> getAllMidia() {
	    Iterable<Entity> entities = Util.listEntities("Midia", null, null);
	    return entities;
	    }

	/**
	 * Get midia entity
	 * 
	 * @param midia_number
	 *          : number of the midia
	 * @return: midia entity
	 */
  public static Iterable<Entity> getMidia(String midia_id) {
	    Iterable<Entity> entities = Util.listEntities("Midia", "midia_id", midia_id);
	    return entities;
	  }
  
  /**
	 * Searches for a cliente and returns the entity as an iterable The search is
	 * key based instead of query
	 * 
	 * @param clienteName
	 *          : username of the cliente
	 * @return the entity with the username as key
	 */
	public static Entity getSingleMidia(String midia_id) {
	    Iterable<Entity> results = Util.listEntities("Midia", "midia_id", midia_id);
	    List<Entity> entity = new ArrayList<Entity>();
	    for(Entity e : results)
	      if(e!=null)
	        entity.add(e);
	      if (!entity.isEmpty()) {
	        return (Entity)entity.remove(0);
	      }
		  return null;
	  }
}
